package com.hctforgreen.greenservice.ui.adapter;

import java.util.List;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.hctforgreen.greenservice.R;
import com.hctforgreen.greenservice.model.BookListEntity.BookEntity;
/**
 * 模糊搜索Lv
 * @author Tallen Zhai
 *
 */
public class FuzzySearchBooksListAdapter extends BaseAdapter {

	private Activity mActivity;
	private ListView mListView;

	private static List<BookEntity> mEntities;
	public FuzzySearchBooksListAdapter(Activity activity, ListView listView,  List<BookEntity> entities) {

		mActivity = activity;
		mListView = listView;
		
		mEntities = entities;
		
		mListView.setAdapter(this);
		mListView.setDivider(mActivity.getResources().getDrawable(R.drawable.ic_driver));
	}

	@Override
	public int getCount() {
		return mEntities.size();
	}

	@Override
	public Object getItem(int position) {
		return mEntities.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewCache viewCache;
		final BookEntity entity = (BookEntity) getItem(position);
		if (convertView == null) {
			convertView = View.inflate(mActivity, R.layout.item_fuzzy_search_book_lst,
					null);
			viewCache = new ViewCache(convertView);
			convertView.setTag(viewCache);
		} else {
			viewCache = (ViewCache) convertView.getTag();
		}
		viewCache.getNameTv().setText(entity.displayName);
//		try {
//			if(entity.bCanUpdate.equals(HctConstants.ON_ACTIVITY_KEY_BOOK_B_CAN_UPDATE_TRUE)){
//				long lCurrentBookFileLongSize = Long.valueOf(entity.currentBookFileLongSize);
//				viewCache.getBarcodeTv().setText(FileCacheUtil.FormetFileSize(lCurrentBookFileLongSize));
//			}else{
//				long lCurrentBookFileLongSize = Long.valueOf(entity.bookFileLongSize);
//				viewCache.getBarcodeTv().setText(FileCacheUtil.FormetFileSize(lCurrentBookFileLongSize));
//			}
//		
//		
//		} catch (Exception e) {
//			// TODO: handle exception
//		}
		return convertView;
	}
	
	
	
	class ViewCache {
		private View baseView;

		private TextView nameTv;

		private TextView barcodeTv;
		
		public ViewCache(View view) {
			baseView = view;
		}
		public TextView getNameTv() {
			if (nameTv == null) {
				nameTv = (TextView) baseView.findViewById(R.id.tv_name);
			}
			return nameTv;
		}
		public TextView getBarcodeTv() {
			if (barcodeTv == null) {
				barcodeTv = (TextView) baseView.findViewById(R.id.tv_barcode);
			}
			return barcodeTv;
		}
	}
}
